//
//  MyInfoViewController.swift
//  weile
//
//  Created by LKM on 16/10/25.
//  Copyright © 2016年 LKM. All rights reserved.
//

import UIKit

/**
 我的资料
 */
class MyInfoViewController: BaseViewController {


    
    @IBOutlet weak var tv_phone: UITextField!
    
    @IBOutlet weak var tv_company: UITextField!
    @IBOutlet weak var tv_address: UITextField!
    @IBOutlet weak var tv_contact: UITextField!
    @IBOutlet weak var tv_contact_phone: UITextField!
    @IBOutlet weak var tv_web: UITextField!
    
    var tvs=[UIView]();
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        
        

//        tvs.append(tv_phone)
        //账号肯定不给编辑
        tv_phone.isUserInteractionEnabled = false;
        
        tvs.append(tv_company)
        tvs.append(tv_address)
        tvs.append(tv_contact)
        tvs.append(tv_contact_phone)
        tvs.append(tv_web)
        
        
        setEditAble(false)
        
        loadData()
    }
    
    
    func setEditAble(_ ise:Bool){
        for tv in tvs{
            tv.isUserInteractionEnabled = ise
        }
        if ise {
            self.navigationItem.rightBarButtonItem =  UIBarButtonItem(title: "完成", style: .plain, target: self, action: #selector(onSubmitUserinfoClick));
        }else{
            self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "btn_edit_bar"), style: .plain, target: self, action: #selector(onEditClick))
            self.navigationItem.leftBarButtonItem?.tintColor=R.colors._939393
        }
    }
    func onEditClick(){
        setEditAble(true)
    }
    func onSubmitUserinfoClick(){
        editUser();
    }
    
    func loadData(){
        ViewHelp.showProgressActivity(containView: self.view)
        let ts=Api.getUserInfo(preTaskSerialNumber: nil) { (re:ResponEntity) in
            ViewHelp.hideProgressActivity(containView: self.view);
            //            self.contentScrollView.endRefreshing(at: self.refresher.position);
            guard re.isSuccess() else{
                ViewHelp.showTipsView(containView: self.view, tips: re.msg)//成功不显示
                return;
            }
            let data = re.getData() as! MyInfoTo;
            
            self.tv_phone.setText(str:data.modeluser?.UserName ?? "");
            //
            //    //都是公司得
            self.tv_company.setText(str:data.modelcompany?.Company_name ?? "");
            self.tv_address.setText(str: data.modelcompany?.Company_address ?? "");
            self.tv_contact.setText(str:data.modelcompany?.Contact_name ?? "");
            self.tv_contact_phone.setText(str:data.modelcompany?.Contact_phone ?? "");
            self.tv_web.setText(str:data.modelcompany?.Company_net ?? "");
            
        }
        taskAlls.append(ts);
        
    }
//    pa.put("company_name", tv_company.getText().toString());
//    pa.put("company_addr", tv_address.getText().toString());
//    pa.put("contact_name", tv_contact.getText().toString());
//    pa.put("contact_tel", tv_contact_phone.getText().toString());
//    pa.put("company_net", tv_web.getText().toString());
    func editUser(){
        ViewHelp.showProgressActivity(containView: self.view)
        let ts=Api.EditUserInfo(preTaskSerialNumber: nil,
                                pa: [
            "company_name":self.tv_company.getText(),
            "company_addr":self.tv_address.getText(),
            "contact_name":self.tv_contact.getText(),
            "contact_tel":self.tv_contact_phone.getText(),
            "company_net":self.tv_web.getText(),
            ]
        ) { (re:ResponEntity) in
            ViewHelp.hideProgressActivity(containView: self.view);
            //            self.contentScrollView.endRefreshing(at: self.refresher.position);
            guard re.isSuccess() else{
                ViewHelp.showTipsView(containView: self.view, tips: re.msg)//成功不显示
                return;
            }
            ViewHelp.showTipsView(containView: self.view, tips: StringHelp.isEmpty(re.msg) ? "修改成功" : re.msg )
            //编辑成功
            self.setEditAble(false);
            
        }
        
        taskAlls.append(ts);
    }
    
    var taskAlls = [Int]();
    deinit {
        //界面退出肯定就不登陆咯
        Api.destroyTask(taskSerialNumbers: taskAlls);
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

import Foundation
import SwiftyJSON

//lkm
class MyInfoTo: NSObject {
    
    var modeluser: MyInfoToModelUserTo?;//
    var modelcus: String?;//
    var modelcompany: MyInfoToModelCompanyTo?;//
    var modelgi: String?;//
    
    //dict["data"].dictionaryObject  dict是JSON
    init?(jsons: [String: JSON]?) {
        super.init()
        if(jsons == nil){
            return nil
        }
        
        if let dict = jsons{
        self.modeluser = MyInfoToModelUserTo.init(jsons: dict["modeluser"]?.dictionary )//(json: JSON() )//dict["modeluser"]?.string;//
        self.modelcus = dict["modelcus"]?.string;//
        self.modelcompany = MyInfoToModelCompanyTo.init(jsons: dict["modelcompany"]?.dictionary  )// dict["modelcompany"]?.string;//
        self.modelgi = dict["modelgi"]?.string;//
        }
    }
    
    
    //initToList( json:JSON(dict["huxing"] ) )  把JSON过的Any转为JSON
    //一定要是JSON要是 [Any] 的话 会装换失败，最后我还是选用这个JSON
    class func initToList(json: JSON)->[MyInfoTo?]{
        if let items = json.array {
            var results = [MyInfoTo?]()
            for item in items {
                let result = MyInfoTo(jsons: item.dictionary )
                //                if(result != nil){//空还加个屁啊  json为“[]” 时 这是不会为空的。除非直接来了个nil
                //                    results.append(result!)
                //                }
                results.append(result)
            }
            return results;
        }
        return [MyInfoTo?]()
    }
    
    //initToList( json:JSON(dict["huxing"] ) )  把JSON过的Any转为JSON
    //一定要是JSON要是 [Any] 的话 会装换失败，最后我还是选用这个JSON
    class func initToMap(json: JSON)->Dictionary<String,MyInfoTo>{
        if let items = json.dictionary {
            var results = Dictionary<String,MyInfoTo>()//取值的时候本身就是可选类型啊,所以这里不用跟数组一样弄可选类型
            for (key, value)  in items {
                let result = MyInfoTo(jsons: value.dictionary )
                results[key] = result
            }
            return results;
        }
        return Dictionary<String,MyInfoTo>()
    }
    

    
    
    
}
import Foundation
import SwiftyJSON

//lkm
class MyInfoToModelUserTo: NSObject {
    
    var MobileInfo: String?;//""
    var Pwd_sign: String?;//""
    var Id: Int?;//4018
    var Answer: String?;//""
    var Email: String?;//""
    var Head_pic: String?;//""
    var Isemail: Int?;//0
    var IsLock: Int?;//0
    var Ismobile: Int?;//0
    var Mobile: String?;//"15011960016"
    var NickName: String?;//"15011960016"
    var Password: String?;//""
    var Question: String?;//""
    var UserName: String?;//"15011960016"
    
    //dict["data"].dictionaryObject  dict是JSON
    init?(jsons: [String: JSON]?) {
        super.init()
        if(jsons == nil){
            return nil
        }
        
        if let dict = jsons{
        
        self.MobileInfo = dict["MobileInfo"]?.string;//""
        self.Pwd_sign = dict["Pwd_sign"]?.string;//""
        self.Id = dict["Id"]?.int;//4018
        self.Answer = dict["Answer"]?.string;//""
        self.Email = dict["Email"]?.string;//""
        self.Head_pic = dict["Head_pic"]?.string;//""
        self.Isemail = dict["Isemail"]?.int;//0
        self.IsLock = dict["IsLock"]?.int;//0
        self.Ismobile = dict["Ismobile"]?.int;//0
        self.Mobile = dict["Mobile"]?.string;//"15011960016"
        self.NickName = dict["NickName"]?.string;//"15011960016"
        self.Password = dict["Password"]?.string;//""
        self.Question = dict["Question"]?.string;//""
        self.UserName = dict["UserName"]?.string;//"15011960016"
        }
    }
    
    
    //initToList( json:JSON(dict["huxing"] ) )  把JSON过的Any转为JSON
    //一定要是JSON要是 [Any] 的话 会装换失败，最后我还是选用这个JSON
    class func initToList(json: JSON)->[MyInfoToModelUserTo?]{
        if let items = json.array {
            var results = [MyInfoToModelUserTo?]()
            for item in items {
                let result = MyInfoToModelUserTo(jsons: item.dictionary )
                //                if(result != nil){//空还加个屁啊  json为“[]” 时 这是不会为空的。除非直接来了个nil
                //                    results.append(result!)
                //                }
                results.append(result)
            }
            return results;
        }
        return [MyInfoToModelUserTo?]()
    }
    
    //initToList( json:JSON(dict["huxing"] ) )  把JSON过的Any转为JSON
    //一定要是JSON要是 [Any] 的话 会装换失败，最后我还是选用这个JSON
    class func initToMap(json: JSON)->Dictionary<String,MyInfoToModelUserTo>{
        if let items = json.dictionary {
            var results = Dictionary<String,MyInfoToModelUserTo>()//取值的时候本身就是可选类型啊,所以这里不用跟数组一样弄可选类型
            for (key, value)  in items {
                let result = MyInfoToModelUserTo(jsons: value.dictionary )
                results[key] = result
            }
            return results;
        }
        return Dictionary<String,MyInfoToModelUserTo>()
    }
    

    
}


import Foundation
import SwiftyJSON

//lkm
class MyInfoToModelCompanyTo: NSObject {
    
    var Uid: Int?;//4018
    var C_time: String?;//"2017-10-25T00:00:00"
    var C_time1: String?;//"2017-10-25T00:00:00"
    var C_time2: String?;//"2017-10-25T00:00:00"
    var C_time3: String?;//"2017-10-25T00:00:00"
    var C_time4: String?;//"2017-10-25T00:00:00"
    var C_time5: String?;//"2017-10-25T00:00:00"
    var Company_address: String?;//""
    var Company_net: String?;//""
    var Company_area: String?;//""
    var Company_class: String?;//""
    var Company_code: String?;//""
    var Company_email: String?;//""
    var Company_fax: String?;//""
    var Company_name: String?;//""
    var Company_phone: String?;//""
    var Contact_address: String?;//""
    var Contact_alipayid: String?;//""
    var Contact_cardid: String?;//""
    var Contact_code: String?;//""
    var Contact_email: String?;//""
    var Contact_mobile: String?;//"15011960016"
    var Contact_name: String?;//""
    var Contact_phone: String?;//"15011960016"
    var Contact_qq: String?;//""
    var Contact_sex: Int?;//"0"
    var Contact_weixin: String?;//""
    var Extension_name: Int?;//0
    var Extension_numberid: Int?;//0
    var Extension_phone: String?;//""
    var Remark: String?;//""
    
    //dict["data"].dictionaryObject  dict是JSON
    init?(jsons: [String: JSON]?) {
        super.init()
        if(jsons == nil){
            return nil
        }
        
        if let dict = jsons{
        
        self.Uid = dict["Uid"]?.int;//4018
        self.C_time = dict["C_time"]?.string;//"2017-10-25T00:00:00"
        self.C_time1 = dict["C_time1"]?.string;//"2017-10-25T00:00:00"
        self.C_time2 = dict["C_time2"]?.string;//"2017-10-25T00:00:00"
        self.C_time3 = dict["C_time3"]?.string;//"2017-10-25T00:00:00"
        self.C_time4 = dict["C_time4"]?.string;//"2017-10-25T00:00:00"
        self.C_time5 = dict["C_time5"]?.string;//"2017-10-25T00:00:00"
        self.Company_address = dict["Company_address"]?.string;//""
        self.Company_net = dict["Company_net"]?.string;//""
        self.Company_area = dict["Company_area"]?.string;//""
        self.Company_class = dict["Company_class"]?.string;//""
        self.Company_code = dict["Company_code"]?.string;//""
        self.Company_email = dict["Company_email"]?.string;//""
        self.Company_fax = dict["Company_fax"]?.string;//""
        self.Company_name = dict["Company_name"]?.string;//""
        self.Company_phone = dict["Company_phone"]?.string;//""
        self.Contact_address = dict["Contact_address"]?.string;//""
        self.Contact_alipayid = dict["Contact_alipayid"]?.string;//""
        self.Contact_cardid = dict["Contact_cardid"]?.string;//""
        self.Contact_code = dict["Contact_code"]?.string;//""
        self.Contact_email = dict["Contact_email"]?.string;//""
        self.Contact_mobile = dict["Contact_mobile"]?.string;//"15011960016"
        self.Contact_name = dict["Contact_name"]?.string;//""
        self.Contact_phone = dict["Contact_phone"]?.string;//"15011960016"
        self.Contact_qq = dict["Contact_qq"]?.string;//""
        self.Contact_sex = dict["Contact_sex"]?.int;//"0"
        self.Contact_weixin = dict["Contact_weixin"]?.string;//""
        self.Extension_name = dict["Extension_name"]?.int;//0
        self.Extension_numberid = dict["Extension_numberid"]?.int;//0
        self.Extension_phone = dict["Extension_phone"]?.string;//""
        self.Remark = dict["Remark"]?.string;//""
        }
    }
    
    
    //initToList( json:JSON(dict["huxing"] ) )  把JSON过的Any转为JSON
    //一定要是JSON要是 [Any] 的话 会装换失败，最后我还是选用这个JSON
    class func initToList(json: JSON)->[MyInfoToModelCompanyTo?]{
        if let items = json.array {
            var results = [MyInfoToModelCompanyTo?]()
            for item in items {
                let result = MyInfoToModelCompanyTo(jsons: item.dictionary )
                //                if(result != nil){//空还加个屁啊  json为“[]” 时 这是不会为空的。除非直接来了个nil
                //                    results.append(result!)
                //                }
                results.append(result)
            }
            return results;
        }
        return [MyInfoToModelCompanyTo?]()
    }
    
    //initToList( json:JSON(dict["huxing"] ) )  把JSON过的Any转为JSON
    //一定要是JSON要是 [Any] 的话 会装换失败，最后我还是选用这个JSON
    class func initToMap(json: JSON)->Dictionary<String,MyInfoToModelCompanyTo>{
        if let items = json.dictionary {
            var results = Dictionary<String,MyInfoToModelCompanyTo>()//取值的时候本身就是可选类型啊,所以这里不用跟数组一样弄可选类型
            for (key, value)  in items {
                let result = MyInfoToModelCompanyTo(jsons: value.dictionary )
                results[key] = result
            }
            return results;
        }
        return Dictionary<String,MyInfoToModelCompanyTo>()
    }
    
    
    
    
}



